package com.cadre.common.utils.oldutil;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.*;
import java.math.BigInteger;

/* loaded from: XWPFHelper.class */
public class XWPFHelper {
    public XWPFDocument createDocument() {
        return new XWPFDocument();
    }

    public XWPFDocument openDoc(String path) throws IOException {
        return new XWPFDocument(new FileInputStream(path));
    }

    public void saveDocument(XWPFDocument document, String savePath) throws IOException {
        OutputStream os = new FileOutputStream(savePath);
        document.write(os);
        os.close();
    }

    public void addParagraphTextHyperlink(XWPFParagraph paragraph, TextStyle textStyle) {
        paragraph.getCTP().addNewHyperlink().setId(paragraph.getDocument().getPackagePart().addExternalRelationship(textStyle.getUrl(), XWPFRelation.HYPERLINK.getRelation()).getId());
        CTText.Factory.newInstance().setStringValue(textStyle.getText());
        CTRPr rpr = CTR.Factory.newInstance().addNewRPr();
        if (textStyle.getFontFamily() != "" && textStyle.getFontFamily() != null) {
            (rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts()).setAscii(textStyle.getFontFamily());
        }
    }

    public void setParagraphSpacingInfo(XWPFParagraph paragraph, TextStyle paragStyle, STLineSpacingRule.Enum lineValue) {
        CTPPr pPPr = getParagraphCTPPr(paragraph);
        CTSpacing pSpacing = pPPr.getSpacing() != null ? pPPr.getSpacing() : pPPr.addNewSpacing();
        if (paragStyle.isSpace()) {
            if (paragStyle.getBefore() != null) {
                pSpacing.setBefore(new BigInteger(paragStyle.getBefore()));
            }
            if (paragStyle.getAfter() != null) {
                pSpacing.setAfter(new BigInteger(paragStyle.getAfter()));
            }
        }
        if (paragStyle.isLine()) {
            if (paragStyle.getLine() != null) {
                pSpacing.setLine(new BigInteger(paragStyle.getLine()));
            }
            if (lineValue != null) {
                pSpacing.setLineRule(lineValue);
            }
        }
    }

    public void setParagraphIndInfo(XWPFParagraph paragraph, TextStyle paragStyle) {
        CTPPr pPPr = getParagraphCTPPr(paragraph);
        CTInd pInd = pPPr.getInd() != null ? pPPr.getInd() : pPPr.addNewInd();
        if (paragStyle.getFirstLine() != null) {
            pInd.setFirstLine(new BigInteger(paragStyle.getFirstLine()));
        }
    }

    public void setParagraphAlignInfo(XWPFParagraph paragraph, ParagraphAlignment pAlign, TextAlignment valign) {
        if (pAlign != null) {
            paragraph.setAlignment(pAlign);
        }
        if (valign != null) {
            paragraph.setVerticalAlignment(valign);
        }
    }

    public static CTPPr getParagraphCTPPr(XWPFParagraph paragraph) {
        CTPPr pPPr = null;
        if (paragraph.getCTP() != null) {
            if (paragraph.getCTP().getPPr() != null) {
                pPPr = paragraph.getCTP().getPPr();
            } else {
                pPPr = paragraph.getCTP().addNewPPr();
            }
        }
        return pPPr;
    }

    public CTRPr getRunCTRPr(XWPFParagraph paragraph, XWPFRun pRun) {
        CTRPr ctrPr;
        if (pRun.getCTR() != null) {
            ctrPr = pRun.getCTR().getRPr();
            if (ctrPr == null) {
                ctrPr = pRun.getCTR().addNewRPr();
            }
        } else {
            ctrPr = paragraph.getCTP().addNewR().addNewRPr();
        }
        return ctrPr;
    }

    public void copyTable(XWPFTable targetTable, XWPFTable sourceTable) {
        targetTable.getCTTbl().setTblPr(sourceTable.getCTTbl().getTblPr());
        for (int i = 0; i < sourceTable.getRows().size(); i++) {
            XWPFTableRow targetRow = targetTable.getRow(i);
            XWPFTableRow sourceRow = sourceTable.getRow(i);
            if (targetRow == null) {
                targetTable.addRow(sourceRow);
            } else {
                copyTableRow(targetRow, sourceRow);
            }
        }
    }

    public void copyTableRow(XWPFTableRow targetRow, XWPFTableRow sourceRow) {
        if (sourceRow != null) {
            targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());
        }
        for (int i = 0; i < sourceRow.getTableCells().size(); i++) {
            XWPFTableCell tCell = targetRow.getCell(i);
            XWPFTableCell sCell = sourceRow.getCell(i);
            if (tCell == sCell) {
                tCell = targetRow.addNewTableCell();
            }
            copyTableCell(tCell, sCell);
        }
    }

    public void copyTableCell(XWPFTableCell targetCell, XWPFTableCell sourceCell) {
        if (sourceCell.getCTTc() != null) {
            targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());
        }
        for (int pos = 0; pos < targetCell.getParagraphs().size(); pos++) {
            targetCell.removeParagraph(pos);
        }
        for (XWPFParagraph sourceParag : sourceCell.getParagraphs()) {
            copyParagraph(targetCell.addParagraph(), sourceParag);
        }
    }

    public void copyParagraph(XWPFParagraph targetParag, XWPFParagraph sourceParag) {
        targetParag.getCTP().setPPr(sourceParag.getCTP().getPPr());
        for (int pos = targetParag.getRuns().size() - 1; pos >= 0; pos--) {
            targetParag.removeRun(pos);
        }
        for (XWPFRun sRun : sourceParag.getRuns()) {
            copyRun(targetParag.createRun(), sRun);
        }
    }

    public void copyRun(XWPFRun targetRun, XWPFRun sourceRun) {
        targetRun.getCTR().setRPr(sourceRun.getCTR().getRPr());
        targetRun.setText(sourceRun.text());
        for (XWPFPicture picture : sourceRun.getEmbeddedPictures()) {
            try {
                copyPicture(targetRun, picture);
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }

    public void copyPicture(XWPFRun targetRun, XWPFPicture sourcePicture) throws InvalidFormatException, IOException {
        XWPFPictureData picData = sourcePicture.getPictureData();
        targetRun.addPicture(new ByteArrayInputStream(picData.getData()), picData.getPictureType(), picData.getFileName(), (int) sourcePicture.getCTPicture().getSpPr().getXfrm().getExt().getCx(), (int) sourcePicture.getCTPicture().getSpPr().getXfrm().getExt().getCy());
    }
}
